package logger

import (
	"log"

	"github.com/go-chi/chi/v5/middleware"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

var (
	_common *zap.Logger
	_sugar  *zap.SugaredLogger
)

func Init(dev bool) {
	if dev {
		tmp, err := zap.NewDevelopment()
		if err != nil {
			log.Fatalf("%v", err)
		}
		_common = tmp
	} else {
		cfg := zap.NewProductionConfig()
		cfg.Encoding = "console"
		cfg.EncoderConfig = zap.NewDevelopmentEncoderConfig()
		tmp, err := cfg.Build(zap.AddCaller(), zap.AddStacktrace(zapcore.ErrorLevel))
		if err != nil {
			log.Fatalf("%v", err)
		}
		_common = tmp
	}
	_sugar = _common.Sugar()
	_sugar.Infof("Logger initialized with level %v", _sugar.Level())
}

func Logger() *zap.SugaredLogger {
	return _sugar
}

func KVLogger() *zap.Logger {
	return _common
}

type radapter struct{}

func (r *radapter) Print(v ...interface{}) {
	_sugar.Debug(v...)
}

func RestLogger() middleware.LoggerInterface {
	return &radapter{}
}
